package com.xiaochong.bitong.api.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaochong.bitong.api.config.hbase.HbaseBaseQueryComponent;
import com.xiaochong.bitong.api.utils.ListSortUtil;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.po.CurrencyInfo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationResultVo;
import com.xiaochong.tir.common.data.vo.app.AppWeightCurrencyInfo;
import com.xiaochong.tir.common.util.RedisKeyApp;
import com.xiaochong.tir.common.util.RedisKeysUtils;

/**
 * @author qzw
 * 2018年8月6日 下午7:59:40
 * @description 
 **/
@Service
public class WeightService {
	
	 @Autowired
	 private RedisServer rediscConfig;
	 
	 @Autowired
	 private CurrencyInfoMapper currencyInfoDao;
	 
	 @Autowired
	 private HbaseBaseQueryComponent hbaseBaseQueryComponent;
	 
	/**
	 * 获取货币信息
	 * @return
	 */
	public List<AppWeightCurrencyInfo> getCurrencyInfo(){
		List<AppWeightCurrencyInfo> list = new ArrayList<AppWeightCurrencyInfo>();
		//查询权重货币
		 CurrencyInfo c = new CurrencyInfo();
		 c.setEnableFlag(true);
		 c.setDeleteFlag(false);
		 c.setWeights(true);
		 List<CurrencyInfo> currencyInfoList = currencyInfoDao.selectByEntityList(c);
		 for(int i = 0; i < currencyInfoList.size(); i++){
			 CurrencyInfo ci = currencyInfoList.get(i);
			 String data = rediscConfig.get(RedisKeysUtils.CURRENCY_MARKET + ci.getCmcId());
			 CurrencyQuotationResultVo cq = JSON.parseObject(data, CurrencyQuotationResultVo.class);
			 AppWeightCurrencyInfo awci = new AppWeightCurrencyInfo();
			 awci.setCmcid(cq.getCmcId());
			 awci.setCurrency_id(cq.getId());
			 awci.setZn_name(cq.getCoinNameCn());
			 awci.setEn_name(cq.getCoinNameEn());
			 awci.setSymbol(cq.getName().split("-")[0]);
			 awci.setMarketCapCny(cq.getMarketCapCny());
			 awci.setVolumn_24h(cq.getVolume24h());
			 
			 String priceRedisKey = RedisKeyApp.COIN_PRICE+cq.getCmcId();
		     String priceRedisData = rediscConfig.get(priceRedisKey);
		     JSONObject ja = JSON.parseObject(priceRedisData);
		     awci.setNewPriceCny(Double.valueOf(ja.get("priceCny").toString()));
		     awci.setNewPriceUsd(Double.valueOf(ja.get("priceUsd").toString()));
		     awci.setRise_24h(Double.valueOf(ja.get("chang24h").toString()));
			 list.add(awci);
		 }
		return list;
	}
	
	/**
	 * 权重货币列表排序
	 * @param column
	 * @param mark
	 * @param list
	 */
	public void sortList(String column, String mark, List<AppWeightCurrencyInfo> list){
		ListSortUtil<AppWeightCurrencyInfo> lsu = new ListSortUtil<AppWeightCurrencyInfo>();
		lsu.mySort(list, column, mark);
	}
}
